package com.yidu.oracle.dao;

import com.yidu.oracle.Tools.DBUtil;
import com.yidu.oracle.model.Member;
import oracle.jdbc.OracleTypes;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName wjy
 * @Description
 * @Author86157
 * @Date 2021/3/22 14:56
 * @Version 1.0
 */
public class RegDao {
    /**
     *增加 注册会员信息
     * @param member
     * @return
     */
    public int deng(Member member) {
        Connection connection=null;
        CallableStatement call=null;
        int rows=0;
        try {
            //连接数据库
            connection=DBUtil.getConnection();
            //数据库增加用户语句
            String sql="{call proc_regInfo(?,?,?,?,?,?)}";
            call=connection.prepareCall(sql);
            //赋值sql语句
            call.setString(1,member.getMemberId());
            call.setString(2,member.getMemberName());
            call.setString(3,member.getSex());
            call.setString(4,member.getPhone());
            call.setString(5,member.getAddress());
            //注册输出参数和返回值
            call.registerOutParameter(6, Types.INTEGER);
            //执行
            call.execute();
            //获取返回的游标对象 游标就是resultSet
            rows= call.getInt(6);
            System.out.println("rows:"+rows);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeSource(connection,call,null);
        }
        return rows;
    }

    /**
     * 判断登录名是否存在
     * @return 用于判断是否存在
     */
    public int select(String loginname) {
        //链接数据库
        Connection connection=null;
        //存储过程和函数 用于子程序
        CallableStatement callableStatement=null;
        //影响的行数
        int row=0;
        try {
            //连接数据库
            connection= DBUtil.getConnection();
            //调用存储过程
            String sql="{call proc_checkName(?,?)}";
            //操作语句
            callableStatement=connection.prepareCall(sql);
            //赋值sql语句
            callableStatement.setString(1,loginname);
            //提示输入的sql语句
            System.out.println("sql语句:"+sql);
            //注册输出参数的返回值
            callableStatement.registerOutParameter(2,Types.INTEGER);
            //执行
            callableStatement.execute();
            //获取返回的值
            row=callableStatement.getInt(2);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeSource(connection,callableStatement,null);
        }
        return row;
    }
}
